package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsRole;
import com.qf.service.AdminRoleService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/admin/role")
public class AdminRoleController {

    @Autowired
    private AdminRoleService adminRoleService;



    /**
     * 查询管理员列表
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param rolename
     * @return
     */
    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = {"角色管理","角色列表"},button = "查询角色列表")
    @GetMapping("/list")
    public Object RoleList(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "20") Integer limit,
                           String sort,String order,String rolename){

        Page<DtsRole> iPage = adminRoleService.selectRoleList(page,limit,sort,order,rolename);

        long total = iPage.getTotal();
        List<DtsRole> records = iPage.getRecords();

        HashMap<String, Object> data= new HashMap<>();
        data.put("total", total);
        data.put("items", records);

        return ResponseUtil.ok(data);
    }


    //管理员列表选项
    @RequestMapping("/options")
    @RequiresPermissions("admin:role:options")
    @RequiresPermissionsDesc(menu = {"角色管理","角色列表"},button = "管理员列表角色选项")
    public Object options(){

        List<DtsRole> options = adminRoleService.selectRoleOptions();
        HashMap [] result = new HashMap[options.size()];

        for (int i = 0; i < options.size(); i++) {
            HashMap<String, Object> data = new HashMap<String,Object>();
            data.put("value", options.get(i).getId());
            data.put("label", options.get(i).getName());
            result[i] = data;
        }
        return ResponseUtil.ok(result);
    }



    @GetMapping("/permission")
    @RequiresPermissions("admin:role:permission")
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "角色授权")
    public Object Permission(Integer roleId){

        return ResponseUtil.ok();
    }


}
